// 询问
const csComfirm = function(text, config = {
	confirmButtonText: "确定",
	cancelButtonText: '取消'
}) {
	//promise 状态
	let promiseStatus = null;
	// 创建promise
	let confirmPromise = new Promise(function(resolve, reject) {
		promiseStatus = {
			resolve,
			reject
		};
	});
	// #ifdef H5
	// 在H5上通过创建dom实现
	let markDom = document.createElement('div');
	markDom.className = "cu-modal show";
	let cuDialogDom = document.createElement('div');
	cuDialogDom.className = "cu-dialog cs-cu-dialog";
	let textDom = document.createElement('div');
	textDom.className = "csComfirmText";
	textDom.textContent = text;
	cuDialogDom.appendChild(textDom);

	// 创建按钮
	let cuBarDom = document.createElement('div');
	cuBarDom.className = "cu-bar cs-cu-bar";
	let btnSureDom = document.createElement('div');
	btnSureDom.className = "confirm-btn-sure";
	let btnCancleDom = document.createElement('div');
	btnCancleDom.className = "confirm-btn-cancle";
	btnCancleDom.textContent = config.cancelButtonText;
	btnSureDom.textContent = config.confirmButtonText;

	btnSureDom.onclick = function() {
		promiseStatus && promiseStatus.resolve();
		markDom.remove();
	}

	btnCancleDom.onclick = function() {
		promiseStatus && promiseStatus.reject();
		markDom.remove();
	}

	cuBarDom.appendChild(btnCancleDom);
	cuBarDom.appendChild(btnSureDom);
	cuDialogDom.appendChild(cuBarDom);



	markDom.appendChild(cuDialogDom);
	document.querySelector("body").appendChild(markDom);
	//#endif

	//#ifdef APP-PLUS
	//在APP中通过plus.nativeObj.View 实现
	console.log("---创建元素---")
	const screenWidth = plus.screen.resolutionWidth;
	const screenHeight = plus.screen.resolutionHeight;
	let popupViewHeight = 150;
	let maskLayer = new plus.nativeObj.View("maskLayer", { //先创建遮罩层
		top: '0px',
		left: '0px',
		height: '100%',
		width: '100%',
		backgroundColor: 'rgba(0,0,0,0.5)'
	});
	// 弹窗内容
	let popupView = new plus.nativeObj.View("popupView", { //创建底部图标菜单
		tag: "rect",
		top: (screenHeight - popupViewHeight) / 2 + "px",
		left: '15%',
		height: popupViewHeight + "px",
		width: "70%"
	});

	popupView.drawRect({
		color: "#FFFFFF",
		radius: "4px"
	}, {
		top: "0%",
		left: "0%",
		height: "100%",
		width: "100%"
	})

	popupView.drawText(text, {
		top: "-50px",
		left: "0px"
	}, {
		size: '16px',
		color: '#333'
	})
	popupView.drawText(config.confirmButtonText, {
		top: "55px",
		left: "30%"
	}, {
		size: '16px',
		color: '#0076F6'
	})
	popupView.drawText(config.cancelButtonText, {
		top: "55px",
		left: "-30%"
	}, {
		size: '16px',
		color: '#333'
	})

	popupView.addEventListener("click", function(e) {
		let buttonWidth = screenWidth * 0.7 / 2;
		if (e.clientY > 100) {
			if (e.clientX > buttonWidth) {
				promiseStatus && promiseStatus.resolve();
			} else {
				promiseStatus && promiseStatus.reject();
			}
			maskLayer.hide();
			popupView.hide();
		}
	});
	// 点击遮罩层
	maskLayer.addEventListener("click", function() { //处理遮罩层点击
		maskLayer.hide();
		popupView.hide();
	});

	maskLayer.show();
	popupView.show();
	//#endif

	return confirmPromise;
}

// 提示
const showToast = function(message, position = 'bottom', icon = 'none') {
	// #ifdef APP-PLUS
	const styles = {
		verticalAlign: String(position)
	}
	plus.nativeUI.toast(message, styles);
	// #endif
	// #ifdef MP || H5
	uni.showToast({
		title: String(message),
		icon: String(icon),
		position: String(position)
	})
	// #endif
}

export default {
	csComfirm,
	showToast
};
